package edu.colorado.csci5548.gps_messenger;

import java.util.*;

public class ContactList 
{
	private Contact _sosContact;
	private List<Contact> _helpContacts;
	private List<Contact> _friendContacts;

	// constructors
	ContactList()
	{
		this(new Contact(""));
	}
	
	ContactList(Contact sosContact)
	{
		_sosContact = new Contact(sosContact);
		_helpContacts = new ArrayList<Contact>(10);
		_friendContacts = new ArrayList<Contact>(100);
	}

	ContactList(ContactList contactList)
	{
		this(contactList.getSOSContact());
		
		Iterator<Contact> helpContactIterator = contactList.helpContactsIterator();
		while(helpContactIterator.hasNext())
		{
			Contact hc = (Contact) helpContactIterator.next();
			_helpContacts.add(hc);
		}
		
		Iterator<Contact> friendContactIterator = contactList.friendContactsIterator();
		while(friendContactIterator.hasNext())
		{
			Contact fc = (Contact) friendContactIterator.next();
			_friendContacts.add(fc);
		}		
	}	
	
	// getters
	public Contact getSOSContact()
	{
		return new Contact(_sosContact);
	}
	
	// setters
	public void setSOSContact(Contact sosContact)
	{
		_sosContact = new Contact(sosContact);
	}
	
	// other methods
	public Iterator<Contact> helpContactsIterator()
	{
	    return _helpContacts.iterator();
	}
	
	public Iterator<Contact> friendContactsIterator()
	{
	    return _friendContacts.iterator();
	}

	public int helpContactListSize()
	{
		return _helpContacts.size();
	}
	
	public int friendContactListSize()
	{
		return _friendContacts.size();
	}
	
	public void addHelpContact(Contact contact)
	{
		_helpContacts.add(contact);
	}
	
	public void addFriendContact(Contact contact)
	{
		_friendContacts.add(contact);
	}
	
	public void removeHelpContact(Contact contact)
	{
		Iterator<Contact> contactIterator = _helpContacts.iterator();
		while(contactIterator.hasNext())
		{
			Contact c = (Contact) contactIterator.next();
			if (c.equals(contact))
			{
				_helpContacts.remove(c);
				break;
			}
		}
	}	
	
	public void removeFriendContact(Contact contact)
	{
		Iterator<Contact> contactIterator = _friendContacts.iterator();
		while(contactIterator.hasNext())
		{
			Contact c = (Contact) contactIterator.next();
			if (c.equals(contact))
			{
				_friendContacts.remove(c);
				break;
			}
		}
	}	
		
	public boolean containsHelpContact(Contact contact)
	{
		Iterator<Contact> contactIterator = _helpContacts.iterator();
		while(contactIterator.hasNext())
		{
			Contact c = (Contact) contactIterator.next();
			if (c.equals(contact))
			{
				return true;
			}
		}
		return false;
	}
	
	public boolean containsFriendContact(Contact contact)
	{
		Iterator<Contact> contactIterator = _friendContacts.iterator();
		while(contactIterator.hasNext())
		{
			Contact c = (Contact) contactIterator.next();
			if (c.equals(contact))
			{
				return true;
			}
		}
		return false;
	}
	
	public String toString()
	{
		return ("SOS Contact: " + _sosContact + "\n" +
				"Help Contacts: " + _helpContacts.toString() + "\n" +
				"Friend Contacts: " + _friendContacts.toString());
	}
}
